<template>
	<view class="main" style="background-color: #e4ecf7;min-height: 100vh;">
		<!-- 移除 v-for 从根元素 -->
		<!-- 在内部容器元素上使用 v-for -->
		<view>
			
		</view>
		<view v-for="(item,index) in productList" :key="index" >
			<view class="user_header_box">
			</view>
			<view class="user_amount_box">
				<view class="user_level">
					<image class="icon" src="/static/tourist.jpg"></image>
					<view class="level_desc_box">
						<view class="title">{{ item.commentUserName }}</view>
						<view class="desc">{{ item.commentLevel }}</view>
					</view>
					<u-icon class="right_icon" name="more-dot-fill"></u-icon>
				</view>
			</view>

			<view class="loan_box">
				<view class="loan_title_box">
					<view class="title" style="color: darkcyan;">
						<span>{{ item.commentStroeProductName }}</span>
					</view>
					<view class="link">
						{{ item.commentCreatetime }}
					</view>
				</view>
				<view class="date" style="margin-left: 36rpx;">
					{{ item.commentContent }}
				</view>
				<uni-group>
					<image :src="item.commentImgUrl" class="image-fixed-size"></image>
				</uni-group>
		<view class="share-like-buttons">
			<view class="share-button">
				分享
				<u-icon name="share" color="#2979ff" size="28"></u-icon>
			</view>
			<view class="like-button">
				点赞
				<u-icon name="thumb-up" color="#2979ff" size="28"></u-icon>
			</view>
		</view>
			</view>
			<view class="foot_line">
			
			</view>
		</view>
		<view v-show="!isEmpty">
			这里空空如也，期待您的首评！
		</view>
	</view>
</template>

<script>
	import { store } from '@/uni_modules/uni-id-pages/common/store.js'
		import { get, post, removeToken } from '@/utils/api.js';
	export default {
		data() {
			return {
				productList:[],
				isEmpty:false,
				commentCreatetime:'',
				commentContent:'',
				
			}
		},
		onLoad() {

		},
		methods: {
			getComment() {
			    post('/comment/getCommentByProductId', {
			     commentStoreProductId: uni.getStorageSync('pid')
			    }, {
			      needToken: true,
			      success: (res) => {
			        // 使用箭头函数或 bind(this) 来确保 this 指向 Vue 实例
			       const sortedData = res.data.sort((a, b) => {
			          const dateA = new Date(a.commentCreatetime);
			          const dateB = new Date(b.commentCreatetime);
			          return dateB - dateA;
			        });
					// 遍历数组并判断 commentUserName 是否为空，并根据 commentLevel 过滤评论对象
					       const filteredData = sortedData.filter(comment => {
					          if (!comment.commentUserName) {
					            comment.commentUserName = '匿名用户';
					          }
					
					          if (comment.commentLevel <= 3) {
					            // 如果 commentLevel 大于 3，则不包括这个对象
					           return false;
					          }
					
					          // 根据 commentLevel 赋值评价类型
					          if (comment.commentLevel > 3) {
					            comment.evaluation = '好评';
					          }
					
					          // 包括这个对象
					          return true;
					        });
					
					        this.productList = filteredData;
					console.log(this.productList)
			        if (sortedData.length > 0) {
			          this.commentCreatetime = sortedData[0].commentCreatetime;
			          this.commentContent = sortedData[0].commentContent;
			        } else {
			          this.commentCreatetime = '';
			          this.commentContent = '';
			        }
					 if(this.productList)
					 {
						this.isEmpty = true
					 }
			
			      },
			      fail: (err) => {
			        console.error('获取产品评论失败:', err);
			      }
			    });
			  },
		},
		
		created() {

			uni.getStorageSync('pid')
			this.getComment()
		}
	}
</script>

<style lang="scss" scoped>
	.image-fixed-size {
	  width: 130px; /* 设定宽度 */
	  height: 130px; /* 设定高度 */
	  object-fit: contain; /* 图片会被缩放到适应容器，不会裁剪图片内容 */
	  border-radius: 10px;
	  margin: 40rpx;
	}
	.user_header_box{
		height: 200rpx;
		width: 100%;
		// background-image: url(../../static/user/head_bg.png);
		background-size: 100% 100%;
		display: flex;
		align-items: center;
		justify-content: left;
		.avatar_box{
			margin-left: 80rpx;
			.img{
				height: 120rpx;
				width: 120rpx;
			}
		}
		.user_info{
			color: #fff;
			margin-left: 20rpx;
			.name{
				
			}
			.uid{
				
			}
		}
	}
	.user_amount_box{
		width: 90%;
		margin: -90rpx auto 0 auto;
		.user_level{
			border-radius: 10rpx 10rpx 0 0;
			background-color: #e2eefe;
			padding: 10rpx 14rpx;
			display: flex;
			align-items: center;
			.icon{
				border-radius: 50%;
				height: 60rpx;
				width: 60rpx;
			}
			.level_desc_box{
				margin-left: 20rpx;
				.title{
					font-size: 28rpx;
				}
				.desc{
					font-size: 20rpx;
					color: #808080;
				}
			}
			.right_icon{
				height: 26rpx;
				width: 16rpx;
				margin-left: auto;
				padding-right: 50rpx;
			}
		}
		.amount_box{
			display: flex;
			align-items: center;
			justify-content: space-between;
			background-color: #fff;
			border-radius: 0 0 10rpx 10rpx;
			padding: 40rpx 60rpx;
			.amount{
				font-size: 46rpx;
			}
			.now_with{
				background-color: #ffa415;
				color: #fff;
				padding: 8rpx 30rpx;
				border-radius: 5rpx;
			}
		}
	}
	.user_tab_box{
		background-color: #fff;
		width: 90%;
		margin: 20rpx auto;
		border-radius: 10rpx;
		padding: 10rpx 20rpx;
		padding-bottom: 20rpx;
		box-sizing: border-box;
		.tab_title{
			font-size: 30rpx;
		}
		.tab_list_box{
			margin-top: 20rpx;
			display: flex;
			flex-wrap: wrap;
			.list_item{
				width: 25%;
				text-align: center;
				margin: 16rpx 0;
				.icon{
					height: 80rpx;
					width: 80rpx;
				}
				.text{
					color: #666666;
					margin-top: -10rpx;
					font-size: 26rpx;
				}
			}
		}
	}
	.loan_box{
		background-color: #fff;
		width: 90%;
		margin: 20rpx auto;
		border-radius: 10rpx;
		padding: 10rpx 20rpx;
		padding-bottom: 20rpx;
		box-sizing: border-box;
		position: relative;
				& > .share-like-buttons {
					position: absolute;
					bottom: 20rpx;
					width: 100%;
					display: flex;
					justify-content: space-between;
					padding: 0 50rpx; /* 添加左右内边距，以便按钮不会紧贴边缘 */
				}
		.loan_title_box{
			display: flex;
			align-items: center;
			padding-top: 6rpx;
			.title{
				font-size: 30rpx;
			}
			.link{
				margin-left: auto;
				color: #797979;
				font-size: 20rpx;
			}
		}
		.loan_list{
			margin-top: 30rpx;
			.list_item{
				padding: 0 10rpx;
				border-bottom: 1px solid #e9e9e9;
				margin-top: 20rpx;
				.item_head{
					display: flex;
					align-items: center;
					.pic{
						height: 40rpx;
						width: 30rpx;
					}
					.loan_info{
						margin-left: 20rpx;
						.name{
							color: #797979;
							font-weight: 600;
						}
						.date{
							color: #797979;
							margin-top: 6rpx;
						}
					}
					.join_btn{
						background-color: #2979ff;
						color: #fff;
						margin-left: auto;
						margin-right: 20rpx;
						padding: 6rpx 30rpx;
						border-radius: 30rpx;
						font-size: 28rpx;
					}
				}

			}
		}
	}
	.foot_line{
		display: flex;
		align-items: center;
		justify-content: center;
		padding-bottom: 20rpx;
		color: #5f5f5f;
		.line{
			width: 200rpx;
			border-bottom: 1px solid #5f5f5f;
		}
	}
	.share-like-buttons {
			.share-button,
			.like-button {
				display: flex;
				align-items: center;
				gap: 12rpx; /* 在文字和图标之间添加一些间隙 */
			}
		}
	</style>
</style>